package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.EventMapper;
import cn.tedu.carehome.foundation.mapper.OlderMapper;
import cn.tedu.carehome.foundation.mapper.OtherMapper;
import cn.tedu.carehome.foundation.pojo.dto.EventAddNewDTO;
import cn.tedu.carehome.foundation.pojo.entity.Event;
import cn.tedu.carehome.foundation.pojo.vo.*;
import cn.tedu.carehome.foundation.service.IEventService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EventServiceImpl implements IEventService {
    @Autowired
    private EventMapper eventMapper;

    @Autowired
    private OlderMapper olderMapper;


    @Autowired
    private OtherMapper otherMapper;


    @Override
    public void addNew(EventAddNewDTO eventAddNewDTO) {
        log.debug("开始处理【添加事件】的业务，参数：{}", eventAddNewDTO);
        // 调用Mapper对象的countByName()检查类别名称是否已经被占用
        String name=eventAddNewDTO.getName();
        Integer countByName=eventMapper.countByName(name);
        if (countByName>0){
            String message="添加事件失败!事件名字已经被使用了!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //把默认审核状态,审核人id,审核结果id传入event对象
        Event event=new Event();
        BeanUtils.copyProperties(eventAddNewDTO,event);

        //staffStandardVO:审核人的具体信息(默认是小型事故,审核人是老人绑定的护工)
        StaffStandardVO staffStandardVO=otherMapper.getStaffByOlderIdReal(eventAddNewDTO.getOlderId());
        //判断是否为空,如果为空,则抛出异常
        if (staffStandardVO==null){
            OlderStandardVO older =  olderMapper.getStandardById(eventAddNewDTO.getOlderId());
            String message="添加事件失败!["+older.getName()+"]还没有绑定员工,请加快安排!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        // (如果是一般事故)审核人是副院长,作为其审核人
        if(eventAddNewDTO.getMatterLevel()==2){
            staffStandardVO = otherMapper.getVicePresidentOne();
        }else if(eventAddNewDTO.getMatterLevel()==3){
            //(如果是大型事故)审核人是院长,作为其审核人
            staffStandardVO = otherMapper.getPresidentOne();
        }

        log.debug("事故审核人信息:{}",staffStandardVO);
        String checkInfo="正在审核";
        Integer state=0;
        event.setCheckId(staffStandardVO.getId());
        event.setCheckInfo(checkInfo);
        event.setState(state);
        log.debug("即将插入数据：{}", event);

        //执行新增业务
        int rows = eventMapper.insert(event);
        if (rows != 1) {
            String message = "添加事件失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }


    }

    @Override
    public void delete(Long id) {
        //检测id是否存在
        EventStandardVO eventStandardVO = eventMapper.getStandardById(id);
        if (eventStandardVO==null){
            String message="删除失败!id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检测其审核状态是不是未审核和正在处理,未审核和未处理的事故无法删除
        Integer state=eventStandardVO.getState();
        if (state==0){
            String message="删除失败!该事故还没有审核,无法删除!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        if (state==1){
            String message="删除失败!该事故还没有处理,无法删除!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }


        //执行删除
        int rows = eventMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除事件失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void setPass(Long id,String checkInfo) {//点击使得事故通过审核
        updateInfoById(id,1,checkInfo);
    }

    @Override
    public void setComplate(Long id,String checkInfo) {//点击使得事件已经完成
        updateInfoById(id,2,checkInfo);
    }


    public void updateInfoById(Long id, Integer state,String checkInfo) {
        String stateText[] = {"未审核","通过审核", "事故处理完成"};
        log.debug("开始处理【使得事故{}】的业务，ID：{}，目标状态：{}", stateText[state], id, stateText);
        //查询是否存在
        EventStandardVO eventStandardVO= eventMapper.getStandardById(id);
        if (eventStandardVO==null){
            String message=stateText[state]+"失败,事故不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        // 判断以上查询结果中的state是否与参数state相同
        if (eventStandardVO.getState()==state){
            String message=stateText[state]+"失败,事故已经"+stateText[state];
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //判断state是否符合逻辑(state0->state1,state1->state2)
        if (state-eventStandardVO.getState()!=1){
            String messege="该事件"+stateText[eventStandardVO.getState()]+",无法改为"+stateText[state]+"状态";
            log.debug(messege);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,messege);
        }

        // 创建Event对象，并将2个参数的值封装进来
        Event event=new Event();
        event.setId(id);
        event.setState(state);
        event.setCheckInfo(checkInfo);
        int rows = eventMapper.update(event);
        if (rows != 1) {
            String message = stateText[state] + "修改失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public EventStandardVO getStandardById(Long id) {
        log.debug("开始处理【查询事故详情】的业务，参数:{}",id);
        //不存在
        EventStandardVO eventStandardVO = eventMapper.getStandardById(id);
        if (eventStandardVO==null){
            String message="查询失败!id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return eventStandardVO;
    }

    @Override
    public List<EventListItemVO> list() {
        log.debug("开始处理【查询事故类别列表】的业务，参数：五参数");
        List<EventListItemVO> list= eventMapper.list();
        return list;
    }
}
